//------------------------------------------------------------------------------------
#include <QPainter>
#include <QPaintEvent>
#include <QKeyEvent>
#include <QtDebug>
#include <QTime>
#include "CGamePlayWidget.h"
#include "common.h"
//------------------------------------------------------------------------------------
CGamePlayWidget::CGamePlayWidget(QWidget *parent) : QWidget(parent) {
    map = new CMap();
    map->load(":/snake/levels/level1.sna");

    snake = new CSnake(START_LEN);
    snake->setPosition(map->getDefaultSnakePos());

	controlPanel = new CControlPanel(START_NBVIE);

    souris = new CSouris();
    newSouris();

    qsrand((uint)QTime::currentTime().msec());

	idxImage = 0;
}
//------------------------------------------------------------------------------------
void CGamePlayWidget::next(void) {
    snake->next();

    if(onSouris()) {
        souris->setVisible(false);
        snake->incLen();

		controlPanel->setNbSouris(snake->getLen()-1);

        emit(mangeSouris());

        newSouris();
    }

    if(onMap(snake->getIndex()) || snake->onBody()) {
        int oldLen = snake->getLen();
        delete snake;

        snake = new CSnake(oldLen);
        snake->setPosition(map->getDefaultSnakePos());

		emit(die());
    }

    repaint();
}
//------------------------------------------------------------------------------------
void CGamePlayWidget::setTemps(int temps) {
	controlPanel->setTemps(temps);
}
//------------------------------------------------------------------------------------
void CGamePlayWidget::setNbVie(int nbVie) {
	controlPanel->setNbVie(nbVie);
}
//------------------------------------------------------------------------------------
void CGamePlayWidget::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    
	painter.setPen(QColor(Qt::white));
    painter.setBrush(QColor(Qt::white));
    painter.drawRect(event->rect());

	controlPanel->draw(&painter);

    map->draw(&painter, idxImage);

    snake->draw(&painter, idxImage);
    if(souris->isVisible()) {
        souris->draw(&painter, idxImage % souris->getNbImage());
    }

	idxImage++;
}
//------------------------------------------------------------------------------------
void CGamePlayWidget::keyPressEvent(QKeyEvent *event) {
    switch(event->key()) {
        case Qt::Key_Left:
            if(snake->getIncX() == 0) {
                snake->setIncX(-1);
            }
            break;
        case Qt::Key_Right:
            if(snake->getIncX() == 0) {
                snake->setIncX(1);
            }
            break;
        case Qt::Key_Up:
            if(snake->getIncY() == 0) {
                snake->setIncY(-1);
            }
            break;
        case Qt::Key_Down:
            if(snake->getIncY() == 0) {
                snake->setIncY(1);
            }
            break;
		case Qt::Key_P:
			emit(pause());
			break;
        default:
            break;
    }
}
//------------------------------------------------------------------------------------
bool CGamePlayWidget::onSouris(void) {
   return snake->onCell(souris->getIndex());
}
//------------------------------------------------------------------------------------
void CGamePlayWidget::newSouris(void) {
    int x, y;
    int index;

    do {
        x=randInt(1, NB_CELL_WIDTH - 2);
        y=randInt(1, NB_CELL_HEIGHT - 2);

        index = y*NB_CELL_WIDTH+x;
    }while(snake->onCell(index) || onMap(index));

    souris->setPosition(QPoint(x, y));
    souris->setVisible(true);
}
//------------------------------------------------------------------------------------
int CGamePlayWidget::randInt(int low, int high) {
	return qrand() % ((high + 1) - low) + low;
}
//------------------------------------------------------------------------------------
bool CGamePlayWidget::onMap(int index) {
    return map->isOnMur(index);
}
//------------------------------------------------------------------------------------
